const { body1} = window.presetVar.body;
const {head1} = window.presetVar.head;

/**
 * 表头合计处理
 */
export function headSumHandle(args, type, areaId) {
    //需要汇总的金额字段
    let bodyFieldArr = ["amount", "ybje", "cjkybje", "zfybje", "hkybje", "bbje",
        "cjkbbje", "zfbbje", "hkbbje", "globalbbje", "globalcjkbbje", "globalzfbbje", "globalhkbbje",
        "groupbbje", "groupcjkbbje", "groupzfbbje", "grouphkbbje", "tax_amount", "orgtax_amount", "grouptax_amount", "globaltax_amount",
        "tni_amount", "orgtni_amount", "grouptni_amount", "globaltni_amount", "vat_amount", "orgvat_amount", "groupvat_amount", "globalvat_amount"];
    let changeHeadItem = {};
    console.log(this,'this===',args)
    let props = this?this.props:args[1]
    bodyFieldArr.forEach((bodyField) => {//循环字段
        let totalMny = 0;
        let headField = bodyField=="amount"?"total":bodyField;//total字段特殊处理
        let scale = props.form.getFormItemsValue(head1,headField).scale;//取精度
        for(let name in window.presetVar.body){//循环业务页签
            let areaId = window.presetVar.body[name];
            if(areaId!=='accrued_verify' && areaId!=='er_bxcontrast' && areaId!=='er_cshare_detail'){
                let rows = props.cardTable.getVisibleRows(areaId);
                for(let i in rows){
                    if (
                        rows[i].values[bodyField] != undefined &&
                        rows[i].values[bodyField] != null &&
                        rows[i].values[bodyField].value != null
                    ) {
                        totalMny += parseFloat(rows[i].values[bodyField].value) * Math.pow(10, scale); //汇总
                    }
                }
            }
        }
        
        totalMny = isNaN(totalMny)?(0).toFixed(scale):(totalMny/Math.pow(10, scale)).toFixed(scale);
        changeHeadItem[headField] = {scale: scale, value: totalMny, display: totalMny};
    });
    
    props.form.setFormItemsValue(head1, changeHeadItem)
    // 新增其他页签删除后发票页签联动删除逻辑
    if (type && type == 'delete') {

        let pk_erminvoice = _.get(args[0], 'values.pk_erminvoice.value', '')
        if (!pk_erminvoice) {
            props.cardTable.delRowByRowId(areaId, args[0].rowid)
            //如果是分摊的删除 判断是否是删除完了
            if (areaId === 'er_cshare_detail') {
                let er_cshare_detail_rows = props.cardTable.getVisibleRows('er_cshare_detail')
                if (!er_cshare_detail_rows.length) {
                    let hideTables = this.state.hideTables
                    hideTables.push('er_cshare_detail')
                    this.setState({ hideTables })
                    this.props.cardTable.setTableData('er_cshare_detail', { rows: [] });
                    this.props.form.setFormItemsValue(head1, { iscostshare: false })
                }
            }
            return
        }
        let invoice_Detail = props.cardTable.getVisibleRows('invoice_Detail')
        const bodyDetailRows = props.cardTable.getVisibleRows(areaId)
        //如果是分摊的删除 判断是否是删除完了
        if (areaId === 'er_cshare_detail') {
            let er_cshare_detail_rows = props.cardTable.getVisibleRows('er_cshare_detail')
            if (!er_cshare_detail_rows.length) {
                let hideTables = this.state.hideTables
                hideTables.push('er_cshare_detail')
                this.setState({ hideTables })
                this.props.cardTable.setTableData('er_cshare_detail', { rows: [] });
                this.props.form.setFormItemsValue(head1, { iscostshare: false })
            }
        }
        if (
            bodyDetailRows.some((item) => {
                return item.values.pk_erminvoice.value === pk_erminvoice;
            })
        ) {
            return;
        }

        let bxToInvoice = window.presetVar.bxToInvoice
        let invioces = bxToInvoice && bxToInvoice[pk_erminvoice]
        let goalInvioce = [] //符合的发票
        invioces && invioces.forEach((itemInvoice, index) => {
            let oriTrs = []
            oriTrs = invoice_Detail.filter((item) => {
                return item.values.pk_erminvoice.value === itemInvoice
            })
            goalInvioce = [...goalInvioce, ...oriTrs]
        })

        if (goalInvioce.length) {
            goalInvioce.forEach((item) => {
                props.cardTable.delRowByRowId('invoice_Detail', item.rowid)
            })

        }

        //如果是分摊的删除 判断是否是删除完了
        if (areaId === 'er_cshare_detail') {
            let er_cshare_detail_rows = props.cardTable.getVisibleRows('er_cshare_detail')
            if (!er_cshare_detail_rows.length) {
                let hideTables = this.state.hideTables
                hideTables.push('er_cshare_detail')
                this.setState({ hideTables })
                this.props.cardTable.setTableData('er_cshare_detail', { rows: [] });
                this.props.form.setFormItemsValue(head1, { iscostshare: false })
            }
        }

    }
}


